package com.rainner.app.action;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.rainner.app.base.action.JsonAction;
import com.rainner.app.server.ReaderServer;

@Results({ 
	@Result(name="LOGININ", type = "redirect", location = "/"),
	@Result(name="OUTLOGIN", type = "redirect", location = "/login.html"),
	@Result(name = "LOGIN",type = "freemarker",  location = "/WEB-INF/content/public/login.ftl")
	})
public class LoginAction extends JsonAction {
	private static final long serialVersionUID = 1L;
	private ReaderServer server = new ReaderServer();
	private String inputUsername;
	private String inputPassword;

	@Action("/loginin")
	public String loginIn() throws SQLException, IOException {
		if(server.checkLogin(inputUsername, inputPassword)){
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute("username", inputUsername);
			return "LOGININ";
		}else{
			return "OUTLOGIN";
		}
		
	}
	@Action("/login")
	public String login(){
		return "LOGIN";
	}
	
	@Action("/outlogin")
	public String outLogin(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("username");
		return "LOGININ";
	}

	public void setInputUsername(String inputUsername) {
		this.inputUsername = inputUsername;
	}

	public void setInputPassword(String inputPassword) {
		this.inputPassword = inputPassword;
	}

}
